library(plyr)
library(estimatr)
library(emmeans)
library(tidyverse)
library(magrittr)
library(broom)
library(metafor)

d1 <- "https://github.com/thomasjwood/full_fact/raw/main/ff_mt.rds" %>% 
  url %>% 
  gzcon  %>% 
  readRDS %>% 
  mutate(
    out_agree = out_agree %>% 
      mapvalues(
        c("Tend to disagree",
          "Tend to agree"),
        c("Disagree",
          "Agree")
      ) %>% 
      factor(
        c("Strongly disagree",
          "Disagree",
          "Neither agree nor disagree",
          "Agree",
          "Strongly agree")
      ),
    out_true = out_true %>% 
      factor(
        c("True",
          "Probably true",
          "Not sure",
          "Probably false",
          "False") %>% 
          rev
      ),
    agree_num = out_agree %>% 
      as.numeric,
    true_num = out_true %>% 
      as.numeric,
    cond = cond %>% 
      factor(
        c("cond_items",
          "cond_misinfo",
          "cond_corr")
      )
  )

cor(d1$agree_num, d1$true_num)

d1$com_num <- d1$agree_num %>% 
  add(
    d1$true_num
  ) %>% 
  divide_by(2)


# table for attrittion

t1 <- d1 %>% 
  filter(
    country %>% 
      equals(
        "nigeria"
      ) %>% 
      not
  ) %>% 
  left_join(
    d1 %>% 
      select(
        caseid, wave, country
      ) %>% 
      filter(
        country %>% 
          equals("nigeria") %>% 
          not
      ) %>% 
      group_by(
        country, caseid
      ) %>% 
      summarize(
        wn = wave %>% 
          unique %>% 
          length
      ) %>% 
      mutate(
        attrit = case_when(
          wn %>% equals(1) ~ "One wave only",
          wn %>% equals(2) ~ "Two waves",
          TRUE ~ NA_character_
        )
      )
  ) %>% 
  group_by(
    country, issue
  ) %>% 
  nest


t1$mods <- t1$data %>% 
  map(
    ~lm_robust(
      com_num ~ cond * attrit, 
      data = .x 
    )
  )


t1$emm <- t1$mods %>% 
  map(
    ~emmeans(
      ., 
      consec ~ cond | attrit
    )
  )

t2 <- t1$emm %>% 
  map2_dfr(
    t1$issue,
    ~extract2(.x, 2) %>% 
      tidy %>% 
      mutate(issue = .y)
  ) %>% 
  mutate(
    contrast = contrast %>% 
      str_detect("cond_items") %>% 
      ifelse("Misinformation effect",
             "Correction effect")
  )


t3 <- t2 %>% 
  nest_by(attrit, contrast)


t4 <- t3 %>% 
  pmap_dfr(
    function(attrit, contrast, data)
      
      rma.uni(
        yi = data$estimate, sei = data$std.error
      ) %>% 
      tidy(conf.int = .95) %>% 
      mutate(
        attrit = attrit,
        contrast = contrast
      )
  )

t2 %>% 
  ggplot() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_dotplot(
    aes(
      attrit, estimate,
      fill = adj.p.value < .05
    ), 
    binaxis = "y", stackdir = "center"
  ) +
  geom_linerange(
    aes(
      attrit, ymin = conf.low, ymax = conf.high
    ),
    data = t4,
    size = 1
  ) +
  geom_point(
    aes(
      attrit, estimate
    ),
    data = t4,
    shape = 21,
    size = 8,
    fill = "white"
  ) +
  geom_text(
    aes(
      attrit, estimate, 
      label = estimate %>% 
        round(2) %>% 
        str_replace("0\\.", ".")
    ),
    size = 3,
    data = t4
    ) +
  coord_flip() +
  facet_wrap(
    ~contrast, 
    scales = "free_x",
    # space = "free_x"
  ) +
  labs(
    x = "",
    y =  ""
  ) +
  scale_fill_manual(
    values = c("grey50", "grey99") %>% 
      rev
  ) +
  theme(
    legend.position = "none"
  )

